package com.example.util.sql;



import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class RowMap implements Serializable {

    private Map<String,Object> stringObjectMap;

    public RowMap(Map<String,Object> stringObjectMap){
        this.stringObjectMap=stringObjectMap;
    }

    public void put(String key,Object val){
        if(this.stringObjectMap!=null){
            this.stringObjectMap.put(key,val);
        }
    }

    public RowMap(){
        this.stringObjectMap=new HashMap<>();
    }

    public boolean isEmpty(String key){
        if(this.stringObjectMap==null){
            return true;
        }
        return this.stringObjectMap.get(key) == null;
    }

    public String getString(String key){
        if(isEmpty(key)){
            return "";
        }
        Object obj=this.stringObjectMap.get(key);
        if(obj instanceof String){
            return (String)obj;
        }
        return  String.valueOf(obj);
    }

    public Double getDouble(String key){
        if(isEmpty(key)){
            return null;
        }
        Object obj=this.stringObjectMap.get(key);
        if(obj instanceof Double){
            return (Double) obj;
        }
        return Double.valueOf(String.valueOf(obj));
    }

    public Float getFloat(String key){
        if(isEmpty(key)){
            return null;
        }
        Object obj=this.stringObjectMap.get(key);
        if(obj instanceof  Float){
            return (Float) obj;
        }
        return Float.parseFloat(String.valueOf(obj));
    }

    public Integer getInteger(String key){
        if(isEmpty(key)){
            return null;
        }
        Object obj=this.stringObjectMap.get(key);
        if(obj instanceof Integer){
            return (Integer) obj;
        }
        return Integer.parseInt(String.valueOf(obj));
    }

    public Boolean getBoolean(String key){
        if(isEmpty(key)){
            return null;
        }
        Object obj=this.stringObjectMap.get(key);
        if(obj instanceof Boolean){
            return  (Boolean) obj;
        }
        return Boolean.valueOf(String.valueOf(obj));
    }

    public JSONArray getJSONArray(String key){
        if(isEmpty(key)){
            return null;
        }
        Object obj=this.stringObjectMap.get(key);
        if(obj instanceof String){
            String str=(String) obj;
            return JSONArray.parseArray(str);
        }
        return null;
    }

    public JSONObject getJSONObject(String key){
        if(isEmpty(key)){
            return null;
        }
        Object obj=this.stringObjectMap.get(key);
        if(obj instanceof String){
            String str=(String) obj;
            return JSONObject.parseObject(str);
        }
        return null;
    }

    public Object getObject(String key){
        if(isEmpty(key)){
            return null;
        }
        return this.stringObjectMap.get(key);
    }

    public Integer getSize(){
        if(this.stringObjectMap==null){
            return 0;
        }
        return this.stringObjectMap.size();
    }
}
